package com.pioneer.gui.invoice.model;

import javax.swing.event.EventListenerList;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;

public abstract class EventBroadcastModel
{ 
    protected EventListenerList _changeListeners=new EventListenerList();         
	
	public void addChangeListener(ChangeListener x)
	{
		_changeListeners.add (ChangeListener.class, x);
		// bring it up to date with current state
        //x.stateChanged(new ChangeEvent(this));
	}

	public void removeChangeListener(ChangeListener x)
	{
		_changeListeners.remove (ChangeListener.class, x);
	}

	// Call this when you know the model is changing
	// Wake up all the listeners registered to this model	
	
	protected void fireChange()
	{
		// Create the event:
		ChangeEvent c = new ChangeEvent(this);
		// Get the listener list
		Object[] listeners = _changeListeners.getListenerList();
		// Process the listeners last to first
		// List is in pairs, Class and instance
		for (int i = listeners.length-2; i >= 0; i -= 2)
		{
			if (listeners[i] == ChangeListener.class)
			{
				ChangeListener cl = (ChangeListener)listeners[i+1];
				cl.stateChanged(c);
			}
		}
	}	
}